package org.company.mars.activemq;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class TopicPublisher{
	
	public void publish() throws JMSException, InterruptedException {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
		Connection connection = factory.createConnection();
		connection.start();
		boolean transacted = false;
		Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
		Destination topic = session.createTopic("topic.message");
		//Destination control = session.createTopic("topic.control");
		MessageProducer publisher = session.createProducer(topic);
		publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
		
		this.sendAndClose(connection, session, publisher);
		connection.stop();
		connection.close();
	}
	
	public void sendAndClose(Connection connection,Session session,MessageProducer publisher) throws JMSException, InterruptedException {
		TextMessage txtMessage = session.createTextMessage();
		txtMessage.setText("good morning,activeMQ");
		publisher.send(txtMessage);
	}
}
